package com.kgc.controller;


import com.kgc.service.IMachiningProviderService;
import entity.Machining;
import entity.ResponseMessage;
import entity.StatusCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.product.machining.MachiningAPI;

/**
 * @program: logAndSign
 *
 * @description: 加工工艺管理提供者
 *
 * @author: Cheng
 *
 * @create: 2022-04-18 17:54
 **/
@Controller
public class MachiningProviderController implements MachiningAPI {

    private Logger logger = Logger.getLogger(this.getClass());


    @Autowired
    private IMachiningProviderService iMachiningProviderService;
    /**
     * 加工工艺检查是否重名
     *
     * @param name
     * @return ResponseMessage
     */
    @Override
    @RequestMapping("selectInsrtCheckName")
    @ResponseBody
    public ResponseMessage selectInsrtCheckName(@RequestParam("name") String name) {
        logger.info("MachiningProviderController selectInsrtCheckName is start ....param:name-->"+name);
        //判断下数据
        ResponseMessage responseMessage = new ResponseMessage();
        if (name==null || "".equals(name)) {
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("MachiningProviderController selectInsrtCheckName param is null");
        }
        responseMessage = iMachiningProviderService.selectInsrtCheckName(name);
        logger.debug("MachiningProviderController selectInsrtCheckName result:responseMessage"+responseMessage);
        return responseMessage;
    }

    /**
     * 添加一条加工管理信息
     * @param machining
     * @return
     */
    @Override
    @RequestMapping("insertMachiningInfo")
    @ResponseBody
    public ResponseMessage insertMachiningInfo(@RequestBody Machining machining){
        logger.info("MachiningProviderController insertMachiningInfo is start ....param:machining-->"+machining);
        //判断下数据
        ResponseMessage responseMessage = new ResponseMessage();
        if (machining==null ) {
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("MachiningProviderController insertMachiningInfo param is null");
        }
        responseMessage = iMachiningProviderService.insertMachiningInfo(machining);
        logger.debug("MachiningProviderController insertMachiningInfo result:responseMessage"+responseMessage);
        return responseMessage;
    }

    /**
     * 分页查询加工工艺信息
     * @param pageNum
     * @param name
     * @param sort
     * @return ResponseMessage
     */
    @Override
    @RequestMapping("selectMachiningInfo")
    @ResponseBody
    public ResponseMessage selectMachiningInfo(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "name",defaultValue = "")String name,
                                               @RequestParam(value = "sort",defaultValue = "ASC")String sort)
    {
        logger.info("MachiningProviderController selectMachiningInfo is start ....param:pageNum:-->"+pageNum + ",name:-->"+name + ",sort"+sort);
        ResponseMessage responseMessage = iMachiningProviderService.selectMachiningInfo(pageNum,name,sort);
        logger.debug("MachiningProviderController selectMachiningInfo result:responseMessage"+responseMessage);
        return responseMessage;
    }


    /**
     * 根据id查询加工工艺信息
     * @param id
     * @return
     */
    @Override
    @RequestMapping("selectMachiningById")
    @ResponseBody
    public ResponseMessage selectMachiningById(@RequestParam("id") String id){
        logger.info("MachiningProviderController selectMachiningById is start....param:id-->" + id);
        //判断下如果市空就返回错误
        if (id == null || "".equals(id)) {
            return new ResponseMessage(false,StatusCode.ERROR,"The param id is null",null);
        }
        ResponseMessage responseMessage = iMachiningProviderService.selectMachiningById(id);
        logger.debug("MachiningProviderController selectMachiningById result:responseMessage"+responseMessage);
        return responseMessage;
    }


    /**
     * 根据id更新加工工艺
     * @param machining
     * @return
     */
    @Override
    @RequestMapping("updateMachiningInfo")
    @ResponseBody
    public ResponseMessage updateMachiningInfo(@RequestBody Machining machining){
        logger.info("MachiningProviderController updateMachiningInfo is start....param:machining-->" + machining);
        ResponseMessage responseMessage = iMachiningProviderService.updateMachiningInfo(machining);
        logger.debug("MachiningProviderController updateMachiningInfo result:responseMessage"+responseMessage);
        return responseMessage;
    }

}
